package com.lottery.system.common.pojo;

import com.lottery.system.common.errorcode.ErrorCode;
import com.lottery.system.common.errorcode.GlobeErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * @description: 统一返回结果
 * @author: xiaoxie
 * @date: 2024/8/27 23:53
 **/
@Data
public class CommonResult <T> implements Serializable {
    /**
     * 返回的错误码
    */
    private Integer code;
    /**
     * 返回的错误描述
     */
    private String msg;
    /**
     * 成功时返回的数据
     */
    private T data;

   /**
    * @description: success时返回的结果
    * @author: xiaoxie
    * @date: 2024/8/27 23:53
    * @param: [data]
    * @return: com.lottery.system.common.pojo.CommonResult<T>
    **/
    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.setCode(GlobeErrorCodeConstants.SUCCESS.getCode());
        result.setMsg(GlobeErrorCodeConstants.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }
     /**
     * Description: 失败时返回的结果
     * @param code msg
     * @return: com.lottery.system.common.pojo.CommonResult<T>
     * Author: xiaoxie
     * Date: 22:44 2024/8/27
     */
    public static <T> CommonResult<T> fail(Integer code, String msg) {
        Assert.isTrue(!GlobeErrorCodeConstants.SUCCESS.getCode().equals(code),"code不可以为成功");
        CommonResult<T> result = new CommonResult<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
     /**
     * @description: 失败时返回的结果
     * @author: xiaoxie
     * @date: 2024/8/27 下午11:49
     * @param: [errorCode] 错误对象
     * @return: com.lottery.system.common.pojo.CommonResult<T>
     **/
    public static <T> CommonResult<T> fail(ErrorCode errorCode) {
        return fail(errorCode.getCode(), errorCode.getMsg());
    }


}
